package org.example.util;

import cn.com.jit.assp.dsign.DSign;
import cn.com.jit.ida.util.pki.encoders.Base64;

import java.io.*;

public class JITUtil {

    static String CONFIG_PATH = "D:/2.properties";//配置文件位置路径


    /**
     * detachSign签名
     *
     * @param content 原始PDF文件流
     * @return
     * @throws IOException
     */
    public static byte[] JitSign(InputStream content) throws IOException {
        DSign signDs = new DSign();
        DSign.init(CONFIG_PATH);
        String signReturn = signDs.detachSign("", toByteArray(content));
        if (signReturn != null) { //返回字符串不为空代表签名成功
            //System.out.println("*************Detach签名成功！*******************");
            return Base64.decode(signReturn);
        } else {
            throw new RuntimeException("签名失败：错误号为：" + signDs.getErrorCode() + "错误描述为: " + signDs.getErrorMessage());
        }
    }


    /**
     * detachSign 源文件签名
     *
     * @param sourceFile PDF源文件路径
     * @param pkcs7File  签名后保存PKCS7类型值的文件路径
     * @return
     * @throws IOException
     */
    public static void JitSign(String sourceFile, String pkcs7File) throws IOException {
        DSign signDs = new DSign();
        DSign.init(CONFIG_PATH);
        signDs.detachSign("", sourceFile, pkcs7File);
        int readNum = 0;
        try (InputStream inputStream = new FileInputStream(pkcs7File)) {
            readNum = inputStream.read();
        }
        if (readNum <= 0) {
            throw new RuntimeException("签名失败：错误号为：" + signDs.getErrorCode() + "错误描述为: " + signDs.getErrorMessage());
        }
        //else {
        //System.out.println("文件签名成功！");
        //}
    }


    /**
     * 使用 byte数组方式进行签名校验
     *
     * @param srccode    未签名的原文
     * @param signReturn 签名值
     */
    public static int JitVerify(byte[] srccode, byte[] signReturn) {
        DSign vSignDs = new DSign();
        vSignDs.initConfig(CONFIG_PATH);
        //1.获取原文-直接赋串
        long verifyResult = vSignDs.verifyDetachedSign(signReturn, srccode);
        if (verifyResult == 0) { //返回0代表验证成功
            return 0;
        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
            return 1;
        }
    }


    /**
     * 使用原始文档方式进行签名校验
     *
     * @param srcCode    未签名的原文
     * @param signReturn 签名值
     */
    public static int JitFileVerify(String srcCode, String signReturn) {
        DSign vSignDs = new DSign();
        vSignDs.initConfig(CONFIG_PATH);
        //1.获取原文-直接赋串
        long verifyResult = vSignDs.verifyDetachedSign(srcCode, "".getBytes(), signReturn);
        if (verifyResult == 0) { //返回0代表验证成功
            return 0;
        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
            return 1;
        }
    }


    /**
     * 输入流转字节数组
     *
     * @param input 输入流
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            while ((read = input.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }
            return output.toByteArray();
        }
    }

}
